// ignore_for_file: constant_identifier_names, unused_element

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:top_foundation/utils/ex/image.dart';
import 'package:top_foundation/utils/theme/theme.dart';
import 'package:top_foundation/utils/util.dart';

enum IButtonContentStyle {
  left_icon_right_text,
  left_text_right_icon,
  top_icon_bottom_text,
  top_text_bottom_icon,
}

class IContentSize extends Size {
  const IContentSize(super.width, super.height);
}

class IButtonSize {
  static const minSize = IContentSize(0, 0);
  static IContentSize maxWidth(double height) => IContentSize(1, height);
  static IContentSize maxHeight(double height) => IContentSize(1, height);
  static IContentSize fitSize(double width, double height) =>
      IContentSize(width, height);
}

class IButtonContent extends StatelessWidget {
  const IButtonContent({
    super.key,
    this.text,
    this.icon,
    this.alignment = IAlignment.center,
    this.textColor,
    this.iconColor,
    this.color,
    this.enable = true,
    this.contentSize = IButtonSize.minSize,
    IButtonContentStyle? contentStyle,
    this.iconSize,
    this.fontSize,
    this.child,
    this.spacing = 3,
    this.textStyle,
    this.weight,
    this.backgroundColor,
    this.padding,
    this.margin,
  })  : assert(icon is! Widget || icon is! String || icon is! IconData,
            'icon must be in `CustomWidget Image Icon`, so icon only can be Widget,String,IconData'),
        // style = style ?? IButtonStyle.enable,
        contentStyle = contentStyle ?? IButtonContentStyle.left_icon_right_text,
        isHorizontal =
            contentStyle == IButtonContentStyle.left_icon_right_text ||
                contentStyle == IButtonContentStyle.left_text_right_icon;
  final String? text;
  final dynamic icon;
  final IAlignment alignment;
  final Size? iconSize;
  final IContentSize contentSize;
  // final IButtonStyle style;
  final IButtonContentStyle contentStyle;
  final Widget? child;
  final double? fontSize;
  final Color? textColor;
  final Color? iconColor;
  final Color? color;
  final bool enable;
  final bool isHorizontal;
  final double spacing;
  final TextStyle? textStyle;
  final double? weight;
  final Color? backgroundColor;
  final EdgeInsets? padding;
  final EdgeInsets? margin;
  @override
  Widget build(BuildContext context) {
    return Container(
      width: (contentSize.width == 0 || contentSize.width == 1)
          ? null
          : contentSize.width,
      height: (contentSize.height == 0 || contentSize.height == 1)
          ? null
          : contentSize.height,
      alignment: contentSize.width == 1 ? Alignment.centerLeft : null,
      padding: padding,
      margin: margin,
      color: backgroundColor,
      child: !isHorizontal
          ? SeparatorColumn(
              mainAxisAlignment: mainAxisAlignment,
              crossAxisAlignment: crossAxisAlignment,
              mainAxisSize: contentSize == IButtonSize.minSize
                  ? MainAxisSize.min
                  : MainAxisSize.max,
              children: _children(),
              separatorBuilder: (index) => SizedBox(
                height: spacing,
              ),
            )
          : SeparatorRow(
              crossAxisAlignment: crossAxisAlignment,
              mainAxisAlignment: mainAxisAlignment,
              mainAxisSize: contentSize == IButtonSize.minSize
                  ? MainAxisSize.min
                  : MainAxisSize.max,
              children: _children(),
              separatorBuilder: (index) => SizedBox(
                width: spacing,
              ),
            ),
    );
  }

  CrossAxisAlignment get crossAxisAlignment {
    if (isHorizontal) {
      switch (alignment) {
        case IAlignment.center:
          return CrossAxisAlignment.center;

        case IAlignment.start:
          return CrossAxisAlignment.start;
        case IAlignment.end:
          return CrossAxisAlignment.end;
      }
    }
    return CrossAxisAlignment.center;
  }

  MainAxisAlignment get mainAxisAlignment {
    if (isHorizontal) {
      switch (alignment) {
        case IAlignment.center:
          return MainAxisAlignment.center;

        case IAlignment.start:
          return MainAxisAlignment.start;
        case IAlignment.end:
          return MainAxisAlignment.end;
      }
    }
    return MainAxisAlignment.center;
  }

  List<Widget> _children() {
    var tc = textColor ?? color ?? ITheme.titleColor;

    var items = [
      if (text != null)
        Flexible(
            child: Text(
          text!,
          style: textStyle ?? TextStyle(fontSize: fontSize ?? 15.sp, color: tc),
        )),
      if (icon != null) _getIcon()!,
    ];
    if (items.length > 1) {
      items.insert(
          1,
          SizedBox(
            width: spacing,
          ));
    }
    if (contentStyle == IButtonContentStyle.left_icon_right_text ||
        contentStyle == IButtonContentStyle.top_icon_bottom_text) {
      return items.reversed.toList();
    }
    return items;
  }

  Widget? _getIcon() {
    var size = iconSize ?? Size(20.r, 20.r);
    var ic = iconColor ?? color ?? ITheme.titleColor;
    if (icon is String) {
      return loadImage(
          url: icon,
          width: size.width,
          height: size.height,
          fit: BoxFit.contain);
    }
    if (icon is IconData) {
      return Icon(
        icon,
        size: size.width,
        color: ic,
        weight: weight,
        fill: 0.5,
      );
    }
    if (icon is Widget) {
      return icon;
    }

    return null;
  }
}

class IButtonTheme {
  static ButtonThemeData theme = ButtonThemeData(
    minWidth: 30.r,
    height: 30.r,
    buttonColor: ITheme.selectedColor,
    focusColor: ITheme.selectedColor,
    disabledColor: ITheme.backgroundColor,
    colorScheme: ColorScheme.light(
      primary: ITheme.selectedColor,
      error: Colors.red,
    ),
  );

  static ButtonStyle style({
    Color? textColor,
    Color? overlayColor,
    Color? backgroundColor,
    Color? borderColor,
    BorderRadius? borderRadius,
    Size? size,
    double? iconSize,
    Color? iconColor,
    EdgeInsets? padding,
    TextStyle? textStyle,
    VisualDensity? visualDensity,
    AlignmentGeometry? alignment,
  }) =>
      ButtonStyle(
        backgroundColor: MaterialStatePropertyAll(backgroundColor),
        foregroundColor: MaterialStatePropertyAll(textColor),
        overlayColor: MaterialStatePropertyAll(overlayColor),
        shape: MaterialStatePropertyAll(
          RoundedRectangleBorder(
            borderRadius: borderRadius ?? BorderRadius.zero,
            side: BorderSide(
                color: borderColor ?? ITheme.borderColor, width: 0.5),
          ),
        ),
        side: MaterialStateBorderSide.resolveWith((states) =>
            BorderSide(color: borderColor ?? ITheme.borderColor, width: 0.5)),
        fixedSize: MaterialStatePropertyAll(size),
        minimumSize: MaterialStatePropertyAll(size),
        maximumSize: MaterialStatePropertyAll(size),
        iconColor: MaterialStatePropertyAll(iconColor),
        iconSize: MaterialStatePropertyAll(iconSize),
        padding: MaterialStatePropertyAll(padding),
        textStyle: MaterialStatePropertyAll(textStyle),
        visualDensity: visualDensity,
        alignment: alignment,
      );
}

class IButton {
  static Widget create({
    Widget? child,
    EdgeInsets? padding,
    double? width,
    double? height,
    Color? backgroundColor,
    Border? border,
    BorderRadius? borderRadius,
    LinearGradient? gradient,
    DecorationImage? backrgroundImage,
    required VoidCallback onPressed,
  }) {
    return InkWell(
      onTap: onPressed,
      borderRadius: borderRadius,
      child: Container(
        width: width,
        height: height,
        padding: padding,
        decoration: BoxDecoration(
          color: backgroundColor,
          gradient: gradient,
          borderRadius: borderRadius,
          border: border,
          image: backrgroundImage,
        ),
        child: child,
      ),
    );
  }

  static Widget image({
    String? url,
    double? iconHeight,
    EdgeInsets? padding,
    required VoidCallback onPressed,
  }) {
    return create(
      onPressed: onPressed,
      padding: padding,
      child: IImage.net(
        url: url,
        height: iconHeight,
      ),
    );
  }

  static Widget text({
    String? text,
    TextStyle? textStyle,
    double? width,
    double? height,
    EdgeInsets? padding,
    Color? backgroundColor,
    Border? border,
    BorderRadius? borderRadius,
    LinearGradient? gradient,
    DecorationImage? backrgroundImage,
    Alignment alignment = Alignment.center,
    required VoidCallback onPressed,
  }) {
    return create(
        onPressed: onPressed,
        width: width,
        height: height,
        padding: padding,
        backgroundColor: backgroundColor,
        border: border,
        borderRadius: borderRadius,
        gradient: gradient,
        backrgroundImage: backrgroundImage,
        child: Column(
          mainAxisAlignment: getMainAligment(alignment),
          crossAxisAlignment: getCrossAligment(alignment),
          children: [
            Text(
              text ?? '',
              style: textStyle,
            )
          ],
        ));
  }

  static Widget icon({
    Icon? icon,
    double? width,
    double? height,
    EdgeInsets? padding,
    Color? backgroundColor,
    Border? border,
    BorderRadius? borderRadius,
    LinearGradient? gradient,
    DecorationImage? backrgroundImage,
    Alignment alignment = Alignment.center,
    required VoidCallback onPressed,
  }) {
    return create(
        onPressed: onPressed,
        width: width,
        height: height,
        padding: padding,
        backgroundColor: backgroundColor,
        border: border,
        borderRadius: borderRadius,
        gradient: gradient,
        backrgroundImage: backrgroundImage,
        child: Column(
          mainAxisAlignment: getMainAligment(alignment),
          crossAxisAlignment: getCrossAligment(alignment),
          children: [
            icon ?? const SizedBox(),
          ],
        ));
  }

  static MainAxisAlignment getMainAligment(Alignment alignment) {
    if (alignment.y == -1) {
      return MainAxisAlignment.start;
    }

    if (alignment.y == 1) {
      return MainAxisAlignment.end;
    }

    return MainAxisAlignment.center;
  }

  static CrossAxisAlignment getCrossAligment(Alignment alignment) {
    if (alignment.x == -1) {
      return CrossAxisAlignment.start;
    }

    if (alignment.x == 1) {
      return CrossAxisAlignment.end;
    }

    return CrossAxisAlignment.center;
  }
}
